//
//  UIView+WSDraw.m
//  CharonTVProject
//
//  Created by karong on 2023/6/15.

#import "UIView+WSDraw.h"

@implementation UIView (WSDraw)

- (void)realSetupRoundedCornersWithCutCorners:(UIRectCorner)rectCorner withRadii:(CGSize)radii {
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:radii];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    self.layer.mask = shape;
}


- (void)setupRoundedCornersWithCutCorners:(UIRectCorner)rectCorner withRadii:(CGSize)radii {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:rectCorner cornerRadii:radii];
        CAShapeLayer* shape = [[CAShapeLayer alloc] init];
        [shape setPath:rounded.CGPath];
        self.layer.mask = shape;
    });
}

- (void)setCornerWithLeftTopCorner:(CGFloat)leftTop
                    rightTopCorner:(CGFloat)rigtTop
                  bottomLeftCorner:(CGFloat)bottemLeft
                 bottomRightCorner:(CGFloat)bottemRight {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        CGFloat width = self.bounds.size.width;
        CGFloat height = self.bounds.size.height;
        UIBezierPath *maskPath = [UIBezierPath bezierPath];
        maskPath.lineWidth = 1.5;
        [[UIColor colorFromHexRGB:@"FAB883"] setStroke];
        //FAB883
        maskPath.lineCapStyle = kCGLineCapRound;
        maskPath.lineJoinStyle = kCGLineJoinRound;
        [maskPath moveToPoint:CGPointMake(bottemRight, height)]; //左下角
        [maskPath addLineToPoint:CGPointMake(width - bottemRight, height)];
        
        [maskPath addQuadCurveToPoint:CGPointMake(width, height- bottemRight) controlPoint:CGPointMake(width, height)]; //右下角的圆弧
        [maskPath addLineToPoint:CGPointMake(width, rigtTop)]; //右边直线
        
        [maskPath addQuadCurveToPoint:CGPointMake(width - rigtTop, 0) controlPoint:CGPointMake(width, 0)]; //右上角圆弧
        [maskPath addLineToPoint:CGPointMake(leftTop, 0)]; //顶部直线
        
        [maskPath addQuadCurveToPoint:CGPointMake(0, leftTop) controlPoint:CGPointMake(0, 0)]; //左上角圆弧
        [maskPath addLineToPoint:CGPointMake(0, height - bottemLeft)]; //左边直线
        [maskPath addQuadCurveToPoint:CGPointMake(bottemLeft, height) controlPoint:CGPointMake(0, height)]; //左下角圆弧
        
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        maskLayer.frame = self.frame;
        maskLayer.path = maskPath.CGPath;
        maskLayer.frame = self.frame;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    });
}

@end
